﻿//-----------------------------------------------------------------------
// Source File: "Settings.cs"
// Create Date: 07/30/2014 10:16 AM
// Last Updated: 07/30/2014 10:16 AM
// Authors(s): nealbailey@hotmail.com
//-----------------------------------------------------------------------
// GNU GENERAL PUBLIC LICENSE
//-----------------------------------------------------------------------
// Revision: Version 3, 29 June 2007
// Copyright © 2007 Free Software Foundation, Inc. http://fsf.org/
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//  GNU General Public License for more details.
//  REFERENCE: http://www.gnu.org/licenses/
//-----------------------------------------------------------------------
// Copyright (c) 2010-2014 Baileysoft Solutions
//-----------------------------------------------------------------------
namespace Baileysoft.Gaming.Utils.Yasm
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using HP.EnterpriseServices.Settings;

    /// <summary>
    /// Custom extension of <see cref="SettingsUI"/>
    /// </summary>
    public class Settings : SettingsUI
    {
        #region Constructors

        /// <summary>
        /// Constructor.
        /// </summary>
        public Settings()
            : base(StorageType.XML)
        {
        }

        #endregion Constructors

        #region Properties

        /// <summary>
        /// Gets folder pairs <see cref="Setting"/>.
        /// </summary>
        public virtual Setting YasmFolderPairs
        {
            get { return Get(KeyNames.YASM_FOLDER_PAIRS); }
            set { Create(value); }
        }

        /// <summary>
        /// The delimiter character(s) which seperate source and target folder in a pair.
        /// </summary>
        public virtual string YasmFolderPairsDelimiter
        {
            get { return GetValue(KeyNames.YASM_FOLDER_PAIRS_DELIMITER); }
            set { Create(KeyNames.YASM_FOLDER_PAIRS_DELIMITER, value); }
        }

        /// <summary>
        /// The font face to use on the list view.
        /// </summary>
        public virtual string YasmListViewFontFace
        {
            get { return GetValue(KeyNames.YASM_LISTVIEW_FONT_FACE); }
            set { Create(KeyNames.YASM_LISTVIEW_FONT_FACE, value); }
        }

        /// <summary>
        /// The font size to use on the list view.
        /// </summary>
        public virtual string YasmListViewFontSize
        {
            get { return GetValue(KeyNames.YASM_LISTVIEW_FONT_SIZE); }
            set { Create(KeyNames.YASM_LISTVIEW_FONT_SIZE, value); }
        }

        #endregion Properties

        #region Methods

        /// <summary>
        /// Converts the folder-pairs setting strings into key-value pairs based on the user defined delimiter string
        /// </summary>
        /// <returns>A key-value dictionary of folder pairs</returns>
        /// <exception cref="System.NullReferenceException">The exception that is thrown when there is an attempt to dereference a null object reference.</exception>
        /// <exception cref="System.ArgumentException">The exception that is thrown when one of the arguments provided to a method is not valid.</exception>
        protected internal virtual SortedList<string, string> ParseYasmFolderPairsToDict()
        {
            var dict = new SortedList<string, string>();
            Setting setting = null;
            if (base.TryGet(KeyNames.YASM_FOLDER_PAIRS, out setting))
            {
                if (string.IsNullOrEmpty(YasmFolderPairsDelimiter))
                    return dict;

                foreach (var value in setting.Values)
                {
                    var arrValues = value.Text.Split(new string[] {YasmFolderPairsDelimiter},
                        StringSplitOptions.RemoveEmptyEntries);

                    if (arrValues.Length < 2)
                        continue;

                    dict.Add(arrValues[0], arrValues[1]);
                }
            }
            return dict;
        }

        /// <summary>
        /// Converts the folder-pairs setting strings into key-value pairs based on the user defined delimiter string
        /// </summary>
        /// <returns>A key-value dictionary of folder pairs</returns>
        /// <exception cref="System.NullReferenceException">Parameter is null.</exception>
        /// <exception cref="System.ArgumentException">options is not one of the <see cref="T:System.StringSplitOptions" /> values.</exception>
        protected internal virtual void SaveFolderPairs(SortedList<string, string> pairs)
        {
            var dict = new SortedList<string, string>();
            Setting setting = null;

            var values = pairs.Select(p => new Value(string.Format("{0}{1}{2}", p.Key, YasmFolderPairsDelimiter, p.Value))).ToList();
            if (!TryGet(KeyNames.YASM_FOLDER_PAIRS, out setting))
            {
                Create(setting.Key,
                       values,
                       "The source and target folder pairs.");
            }
            else
            {
                setting.Values.Clear();
                setting.Values = values;
            }
        }

        /// <summary>
        /// Create the default set of settings (concrete).
        /// </summary>
        /// <exception cref="System.ArgumentException">The exception that is thrown when one of the arguments provided to a method is not valid.</exception>
        /// <exception cref="System.ArgumentNullException">The exception that is thrown when a null reference is passed to a method that does not accept it as a valid argument.</exception>
        /// <exception cref="System.NullReferenceException">The exception that is thrown when there is an attempt to dereference a null object reference.</exception>
        protected override void CreateDefaultSettings()
        {
            Setting setting = null;

            // Over-ride SettigsUI 8pt font size
            if (!TryGet(KeyNames.UIFONTSIZE, out setting, "9"))
                Create(setting.Key,
                       setting.Value.Text,
                       "The size of UI fonts.");

            if (!TryGet(KeyNames.YASM_FOLDER_PAIRS_DELIMITER, out setting, "|"))
                Create(setting.Key,
                       setting.Value.Text,
                       "The delimiter character(s) which seperate source and target folder in a pair.");

            if (!TryGet(KeyNames.YASM_LISTVIEW_FONT_FACE, out setting, "Courier New"))
                Create(setting.Key,
                       setting.Value.Text,
                       "The font face to use on the list view.");

            if (!TryGet(KeyNames.YASM_LISTVIEW_FONT_SIZE, out setting, "8"))
                Create(setting.Key,
                       setting.Value.Text,
                       "The font size to use on the list view.");

            base.CreateDefaultSettings();
        }

        #endregion Methods

        #region Nested Types

        /// <summary>
        /// Strongly typed key names of serialized settings
        /// </summary>
        public class KeyNames : SettingsUI.UIKeyNames
        {
            #region Fields

            /// <summary>
            /// YASM specific key setting.
            /// </summary>
            public const string YASM_FOLDER_PAIRS = "yasmFolderPairs";

            /// <summary>
            /// YASM specific key setting.
            /// </summary>
            public const string YASM_FOLDER_PAIRS_DELIMITER = "yasmFolderPairsDelimiter";

            /// <summary>
            /// YASM specific key setting.
            /// </summary>
            public const string YASM_LISTVIEW_FONT_FACE = "yasmListViewFontFace";

            /// <summary>
            /// YASM specific key setting.
            /// </summary>
            public const string YASM_LISTVIEW_FONT_SIZE = "yasmListViewFontSize";

            #endregion Fields

            #region Constructors

            /// <summary>
            /// Default parameterless constructor.
            /// </summary>
            protected internal KeyNames()
                : base()
            {
            }

            #endregion Constructors
        }

        #endregion Nested Types
    }
}